package club.ruanx.auth.vcode.service;

import club.ruanx.auth.vcode.ImageCodeProperties;
import club.ruanx.auth.vcode.ValidateCode;
import club.ruanx.constraint.RedisConstraint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author 阮胜
 * @date 2018/10/5 21:35
 */
@Component
public class VcodeRedisServiceImpl implements VcodeRedisService {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private ImageCodeProperties imageCodeProperties;

    @Override
    public void saveValidateCode(String vid, ValidateCode code) {
        redisTemplate.opsForValue().set(RedisConstraint.VALIDATE_CODE + vid, code, imageCodeProperties.getExpiredInSeconds(), TimeUnit.MINUTES);
    }

    @Override
    public ValidateCode getValidateCode(String vid) {
        return (ValidateCode) redisTemplate.opsForValue().get(RedisConstraint.VALIDATE_CODE + vid);
    }

    @Override
    public void deleteValidateCode(String vid) {
        redisTemplate.delete(RedisConstraint.VALIDATE_CODE + vid);
    }
}
